import importlib
from reader.excel_reader import ExcelDataParser
from reader.excel_reader import ExcelIndexParse
from misc.utils import write_to_file, ensure_file_path, get_dir_files

import argparse
import os

cfg_tab = ' ' * 4
cfg_line = '\n'


def get_server_render(render_name):
    render_module = importlib.import_module(f'render.lua_render')
    render_func = getattr(render_module, f'{render_name}_render')
    return render_func

def get_client_render():
    pass

def get_convert(convert_name):
    covert_module = importlib.import_module(f'converter.{convert_name}_convert')
    convert_func = getattr(covert_module, 'convert')
    return convert_func

def export_csv(csv_file, server_output_path, client_output_path):
    #1 读取index
    index_parse = ExcelIndexParse(csv_file)
    sheet_index_map = index_parse.parse()
    if not sheet_index_map:
        print('no index sheet')
        return
    
    #2 读取sheet数据
    sheet_data_map = {}
    for sheet_name, sheet_index in sheet_index_map.items():
        parser = ExcelDataParser(csv_file)
        parser.parse(sheet_name, sheet_index["export"])
        sheet_data_map[sheet_name] = parser

    #2 业务系统做数据转换
    sheet_convert_data_dict = {}
    for sheet_name, parser in sheet_data_map.items():
        sheet_index = sheet_index_map[sheet_name]
        convert_func = get_convert(sheet_index['convert'])
        if not convert_func:
            print(f'convertor not found sheet_name:{sheet_name} {sheet_index["convert"]}')
            return
        
        server_data = convert_func(parser.server_data, 's')
        client_data = convert_func(parser.client_data, 'c')
        sheet_convert_data_dict[sheet_name] = {
            'server': server_data,
            'client': client_data
        }
    
    #3 生成lua代码
    for sheet_name, sheet_data in sheet_convert_data_dict.items():
        server_render = get_server_render(sheet_index_map[sheet_name]['render'])
        server_code = server_render(sheet_name, sheet_data['server'])
        write_to_file(f'{server_output_path}/{sheet_name}.lua', server_code)

        # client_render = get_server_render(sheet_index_map[sheet_name]['render'])
        # client_code = client_render(sheet_name, sheet_data['client'])
        # write_to_file(f'{args.client_path}/{sheet_name}.lua', client_code)




#  py app_cmd.py --excel_path excel --excel_file Item/CurrencyBase2.xlsx --server_path output/Server --client_path output/Client
if __name__ == '__main__':
    arg_parser = argparse.ArgumentParser()
    # 三个参数，输入文件，服务器输出路径，客户端输出路径
    arg_parser.add_argument('--excel_path', help='excel_path')
    arg_parser.add_argument('--excel_file', help='input excel file', required=False)
    arg_parser.add_argument('--server_path', help='server output path')
    arg_parser.add_argument('--client_path', help='client output path')
    args = arg_parser.parse_args()
    
    if not args.excel_file:
        file_list = get_dir_files(args.excel_path, '.xlsx')       
        # for file in file_list:
        #     print(f'file = {file}')
        exit(0)

    file_name = os.path.splitext(args.excel_file)[0]
    excel_full_path = os.path.join(args.excel_path, args.excel_file)
    server_output_path = os.path.join(args.server_path, f'{file_name}.lua')
    client_output_path = os.path.join(args.client_path, f'{file_name}.lua')
    export_csv(excel_full_path, args.server_path, args.client_path)

